<?php
namespace App\Http\Controllers;

use App\Http\Model\FormatCheck;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\GiftBagIntimacyServices;
use App\Http\Services\GiftBagRechargeLogServices;
use App\Http\Services\LoverRechargeServices;
use App\Http\Services\RegisterServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

//情人节
class LoverController extends Controller
{

    protected $configServices;

    public function __construct()
    {
        parent::__construct();
        //监听充值
        $this->configServices = new ConfigServices();
        $this->loveRechargeServices = new LoverRechargeServices();
        $this->userServices = new UserServices();
        $this->registerService = new RegisterServices();
//        $this->assignAction();
    }

    public function assignAction($body)
    {
        if ($body['consumer_type'] == 2) {
            $body_date = $body['pay_timestamp'];
        } else {
            $body_date = $body['change_timestamp'];
        }
        $consumer_date = $body_date ?? time();
        $config = $this->configServices->getConfig(4);
        $checkDate = $this->configServices->checkConfigDate($config['lover_date'], $consumer_date);
        if (!$checkDate) {
            write_log('loverConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        if ($body['consumer_type'] == 1) {
            $this->listenInviteUser($body);
        }
        if ($body['consumer_type'] == 2) {
            $this->listenUserRecharge($body);
        }
        if ($body['consumer_type'] == 3) {
            $this->listenDiamondConsumer($body);
        }
        return true;
    }

    //监听充值
    public function listenUserRecharge($body)
    {

        $user_id = $body['user_id'] ?? 0;
        $money = $body['price'] / 100 ?? 0;//正式
        $now_date = $body['pay_timestamp'] ?? time();
        if ($money < 30) {
            write_log('loverConsumer', 'rechargeMoneyError', $body);//充值金额不达标
            return false;
        }
        //获取当天用户充值记录
        $now_log = $this->loveRechargeServices->getUserLog($user_id, date('Y-m-d', $now_date), $body);
        if (!$now_log) {
            write_log('loverConsumer', 'rechargeUserLogError', $body);//用户充值记录添加失败
            return false;
        }
        write_log('loverConsumer', 'rechargeUserLogSuccess', $body);//用户充值记录添加失败
        return true;
    }

    //用户礼包领取列表
    public function userGiftList(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $checkUser = $this->userServices->getKkaudioUserInfo($user_id);
        if (!$checkUser) {
            OutJson::toJson(-1, '用户信息错误');
        }
        $res = $this->loveRechargeServices->getUserGift($user_id);
        if (!$res) {
            OutJson::toJson(-1, '无用户充值记录');
        }
        OutJson::toJson(200, '成功', $res);
    }

    //点击领取
    public function clickGiftBag(Request $request)
    {
        $config = $this->configServices->getConfig(4);
        $checkDate = $this->configServices->checkConfigDate($config['lover_date'], time());
        if (!$checkDate) {
            OutJson::toJson(410, '活动已经结束');
        }
        $user_id = $request->input('user_id') ?? 0;
        $checkUser = $this->userServices->getKkaudioUserInfo($user_id);
        if (!$checkUser) {
            OutJson::toJson(-1, '用户信息错误');
        }
        $rank = $request->input('rank') ?? 1;
        $res = $this->loveRechargeServices->clickGift($user_id, $rank);
        OutJson::toJson($res['code'], $res['msg']);
    }

    //监听用户消费钻石
    public function listenDiamondConsumer($body)
    {
        $user_id = $body['user_id'] ?? 0;
        $diamond = $body['diamond_value'] ?? 0;
        if ($diamond > 0) {
            write_log('loverConsumer', 'diamondTypeError', $body);//非钻石消耗
            return false;
        }
        $res = $this->loveRechargeServices->consumerLog($user_id, $diamond);
        if (!$res) {
            write_log('loverConsumer', 'updateDiamonedValueError', $body);//钻石消耗更新
            return false;
        }
        write_log('loverConsumer', 'updateDiamonedValueSuccess', $body);//钻石消耗类型错误
        return true;
    }

    //用户点击领取
    public function clickDiamondGift(Request $request)
    {
        $rank = $request->input('rank') ?? 1;


        $user_id = $request->input('user_id') ?? 0;
        $checkUser = $this->userServices->getKkaudioUserInfo($user_id);
        if (!$checkUser) {
            OutJson::toJson(-1, '用户信息错误');
        }
        $res = $this->loveRechargeServices->clickDiamondGift($user_id, $rank);
        OutJson::toJson($res['code'], $res['msg']);
    }

    //翻牌列表
    public function diamondList(Request $request)
    {
        //type 0 七夕快乐  1气泡 2头饰 3金豆  4靓号
        //value    0         7      7    7/77/520/777      120000c
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->loveRechargeServices->getDiamondList($user_id);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    //邀请注册
    public function inviteRegister(Request $request)
    {
        $phone = $request->post('phone');
        $sms_code = $request->post('code');
        $password = $request->post('password');
        $user_id = $request->post('user_id'); // 主框架 uuid
        if (empty($user_id)) {
            $user_id = 0;
        }
        //邀请用户
        if (!$phone || !FormatCheck::checkPhone($phone)) {
            OutJson::toJson(-1, '手机号格式错误');
        }
        if (!$sms_code || !$password) {
            OutJson::toJson(-1, '参数错误');
        }
        if (!FormatCheck::checkPassword($password)) {
            OutJson::toJson(-1, '密码长度必须由6-16位数字字母组成，不能包含特殊字符');
        }
//        审核通过 传输register api
        $register = $this->loveRechargeServices->send_register($phone, $sms_code, $password, $user_id);
        if ($register['code'] != 200) {
            OutJson::toJson($register['code'], $register['msg'], $register['data']);
        }
        $new_uid = $register['data']['user_id'];//新用户id
        if ($user_id == 0) {
            write_log('loverConsumer', 'noInviteUid', $new_uid);
            OutJson::toJson(200);
        }
        //判断注册是否过期
        $config = $this->configServices->getConfig(4);
        $checkDate = $this->configServices->checkConfigDate($config['lover_date'], time());
        if (!$checkDate) {
            write_log('loverConsumer', 'inviteUserError', $user_id);//活动时间错误
            OutJson::toJson(200);
        }
        //注册成功 绑定用户关系
        $createUser = $this->loveRechargeServices->createInviteUser($new_uid, $user_id);
        if (!$createUser) {
            //记录日志
            write_log('register', 'createUserError', $new_uid);
            //api入库失败
            OutJson::toJson(401, '用户生成失败');
        }
//        注册完毕
        OutJson::toJson(200);
    }

    //监听登录
    public function listenInviteUser($body)
    {
        $user_id = $body['user_id'];
        $invite_uid = $body['invite_uid'];
        return $this->loveRechargeServices->checkUser($user_id, $invite_uid);
    }

    public function getUserFowl(Request $request)
    {
        $user_id = $request->input('user_id');
        $res = $this->loveRechargeServices->getUserFowlNum($user_id);
        OutJson::toJson(200, '成功', $res);
    }

    public function convertFowl(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $type = $request->input('type') ?? 1; //1 亲密值 2名片
        $num = $request->input('num') ?? 1; //输入数量
        if ($type == 2) {
            $num = 20;
        }
        $res = $this->loveRechargeServices->convert($user_id, $type, $num);
        OutJson::toJson($res['code'], $res['msg']);
    }

    public function clickEgg(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $res = $this->loveRechargeServices->getUserEgg($user_id);
        OutJson::toJson($res['code'], $res['msg']);
    }

    public function cpEgg()
    {
        return $this->loveRechargeServices->cpEgg();
    }

    public function getShareConfig(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $user_info = $this->loveRechargeServices->getUserInfo($user_id);
        $config_data = $this->configServices->getConfig(4);
        $data['user_id'] = $user_id;
        $data['image'] = $user_info['head_image'];
        $data['title'] = $config_data['lover_kk_wx_title']['value'] ?? '壳壳分享';
        $data['desc'] = $config_data['lover_kk_wx_desc']['value']  ?? '欢迎来到壳壳语音';
        OutJson::outJsonNoData(Hint::SUCCESS, $data);
    }

}